package com.hh.system.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hh.baseframework.util.Convert;
import com.hh.baseframework.util.FileUtil;
import com.hh.baseframework.util.statics.StaticVar;
import com.hh.baseframework.util.statics.SysParam;
import com.hh.hibernate.dao.inf.IHibernateDAO;
import com.hh.message.task.SysMessageTask;
import com.hh.system.bean.HhSysParam;
import com.hh.system.task.FileCopyDeleteTask;
import com.hh.usersystem.bean.usersystem.HHXtZmsx;
import com.hh.usersystem.bean.usersystem.HhXtYh;

@Service
public class DataInitializationService {
	@Autowired
	private IHibernateDAO<HhSysParam> sysParamDao;
	@Autowired
	private IHibernateDAO<HhXtYh> xtyhdao;
	private static final Logger logger = Logger
			.getLogger(DataInitializationService.class);

	@Transactional
	public void dataInitialization() throws IOException {
		initProperties();
		initSysParam();
		initSysUser();
		initTask();
	}

	private void initTask() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		Timer sysMessageTask = new Timer();
		sysMessageTask.schedule(new SysMessageTask(), cal.getTime(),
				24 * 60 * 60 * 1000);

		Timer fileCopyDeleteTask = new Timer();
		fileCopyDeleteTask.schedule(new FileCopyDeleteTask(), cal.getTime(),
				24 * 60 * 60 * 1000);
	}

	private void initSysUser() {
		HhXtYh hhXtYh = new HhXtYh();
		hhXtYh.setDsr(new Date());
		hhXtYh.setNxb(1l);
		hhXtYh.setNzt(0l);
		hhXtYh.setId("92b38970-69d8-4f5e-b7fc-f06f458a9f1f");
		hhXtYh.setText("超级管理员");
		hhXtYh.setVdh("000000");
		hhXtYh.setVdlzh("admin");
		hhXtYh.setVdzyj("admin@hh.com");
		hhXtYh.setVcreate("system");
		hhXtYh.setVupdate("system");
		hhXtYh.setVorgid("system");
		hhXtYh.setDcreate(new Date());
		hhXtYh.setDupdate(new Date());
		hhXtYh.setOrder(new Date().getTime()
				+ UUID.randomUUID().toString().replace("-", ""));
		HHXtZmsx hhXtZmsx = new HHXtZmsx();
		hhXtYh.setHhXtZmsx(hhXtZmsx);
		hhXtYh.setVmm("123456");
		hhXtYh.setId("92b38970-69d8-4f5e-b7fc-f06f458a9f1f");
		hhXtZmsx.setId(hhXtYh.getId());

		// HhXtYhOrg hhXtYhOrg = new HhXtYhOrg();
		// hhXtYhOrg.setYhId(hhXtYh.getId());
		// hhXtYhOrg.setOrgId("7c1fbf4b-d78d-4e07-a790-4892d985fa51");
		// hhXtYhOrg.setVcreate("system");
		// hhXtYhOrg.setVupdate("system");
		// hhXtYhOrg.setVorgid("system");
		// hhXtYhOrgDAO.createEntity(hhXtYhOrg);
		// HhXtYhJs hhXtYhJs = new HhXtYhJs();
		// hhXtYhJs.setYhId(hhXtYh.getId());
		// hhXtYhJs.setJsId("439da940-b607-4f5d-bb0a-a165e4b34001");
		// // hhXtYh.getJsList().add(jsid);
		// hhXtYhJs.setVcreate("system");
		// hhXtYhJs.setVupdate("system");
		// hhXtYhJs.setVorgid("system");
		// hhXtYhJsDAO.createEntity(hhXtYhJs);
		xtyhdao.saveOrUpdateEntity(hhXtYh);
	}

	private void initSysParam() {
		List<HhSysParam> hhSysParams = sysParamDao.queryList(HhSysParam.class);
		if (hhSysParams.size() == 0) {
			HhSysParam hhSysParam = new HhSysParam();
			hhSysParam.setSysName("信息管理系统");
			hhSysParam.setSysIcon("/hhcommon/images/extjsico/17460336.png");
			sysParamDao.createEntity(hhSysParam);
			SysParam.hhSysParam = hhSysParam;
		} else {
			SysParam.hhSysParam = hhSysParams.get(0);
		}

	}

	private void initProperties() throws IOException {
		ClassLoader classloader = Thread.currentThread()
				.getContextClassLoader();

		Properties p = new Properties();
		InputStream in = classloader.getResourceAsStream("system.properties");
		p.load(in);

		Properties p2 = new Properties();
		InputStream in2 = classloader.getResourceAsStream("spring.properties");
		p2.load(in2);

		Object dialect = p2.get("hibernate.dialect");
		String dialectString = Convert.toString(dialect);
		if (dialectString.toLowerCase().indexOf("mysql") > -1) {
			SysParam.DATABASE = "mysql";
		} else if (dialectString.toLowerCase().indexOf("oracle") > -1) {
			SysParam.DATABASE = "oracle";
		}
		SysParam.DATABASE_SCHEMA = p2.get("hibernate.default_schema");
		SysParam.COMMON_PROJECT_NAME = p.getProperty("COMMON_PROJECT_NAME");

		String classPath = Thread.currentThread().getContextClassLoader()
				.getResource("").getPath();
		classPath = classPath.substring(1, classPath.indexOf("/WEB-INF"));
		StaticVar.contextPath = classPath;
		logger.info("项目绝对路径：" + StaticVar.contextPath);
		classPath = classPath.substring(0, classPath.lastIndexOf("/"));
		StaticVar.webappPath = classPath;
		logger.info("web服务器绝对路径：" + StaticVar.webappPath);
		StaticVar.filepath = StaticVar.webappPath + StaticVar.filebasepath;
		logger.info("附件绝对路径：" + StaticVar.filepath);
		FileUtil.isExist(StaticVar.filepath);
		FileUtil.isExist(StaticVar.filepath + "/task");
	}

}
